//
//  HTInfoViw.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/28.
//

import SwiftUI

struct HTInfoView: View {

  @Binding var var_selEp: HTEp!
  @Binding var var_source: String
  @Binding var var_serverUrlName: String

  @State private var var_animeInfo: HTAnime! = HTAnime()
  @State private var var_serverUrls: [HTServer]!
  @State private var var_onSelect: ((String) -> Void)?
  @State private var var_infoDetail: String!
  @State private var var_title: String!
  @State private var var_showPushToFeedback: Bool = false

  init(var_animeInfo: HTAnime!, var_serverUrls: [HTServer]!, var_serverUrlName: Binding<String>, var_selEp: Binding<HTEp?>, var_source: Binding<String>, var_onSelect: ((String) -> Void)? = nil) {
    _var_animeInfo = State(initialValue: var_animeInfo)
    _var_serverUrls = State(initialValue: var_serverUrls)
    _var_onSelect = State(initialValue: var_onSelect)
    _var_selEp = var_selEp
    _var_source = var_source
    _var_serverUrlName = var_serverUrlName

    if (var_animeInfo.var_ep != nil) {
      _var_title = State(initialValue: var_animeInfo.var_title ?? "")
      _var_infoDetail = State(initialValue: var_animeInfo.var_ep! + " / " + var_animeInfo.var_pub_date! + " / " + (var_animeInfo.var_genres ?? "").ht_infoDetail())
    }
  }

  var body: some View {
    ZStack{
      NavigationLink(destination: HTFeedbackScreen(), isActive: $var_showPushToFeedback) {
        EmptyView()
      }

      VStack(alignment: .leading, spacing: 0) {
        HStack(spacing: 0) {
          Text("Server:")
            .ht_commonTextStyle(color: Color(hex: var_darkColor), fontSize: var_h3, weight: .bold)
          ForEach(0..<(var_serverUrls ?? []).count, id:\.self) { index in
            let var_item = var_serverUrls?[index]
            Button() {
              if (var_serverUrlName != var_item!.var_name) {
                var_serverUrlName = var_item!.var_name
                var_onSelect!(var_item?.var_url ?? "")
              }
            } label: {
              Text(var_item?.var_name ?? "")
                .ht_commonTextStyle(color: var_serverUrlName == var_item!.var_name ? Color(hex: var_purpleColor) : Color(hex: var_lightColor), fontSize: var_h3, weight: .bold)
                .padding(.horizontal, 12)
                .padding(.vertical, 10)
            }
          }
        }
        Text(var_title ?? "")
          .ht_commonTextStyle(color: Color(hex: var_darkColor), fontSize: var_h3, weight: .bold)
          .lineLimit(1)
          .padding(.top, 10)
        Text(var_infoDetail ?? "")
          .ht_commonTextStyle(color: Color(hex: var_lightColor), fontSize: var_h5, weight: .none)
          .lineLimit(1)
          .padding(.top, 6)
        HStack(spacing: 0){
          HTShareButton(var_shareIcon: "share_black", var_onShare: {
            HTNetworkManager().ht_requestForBuried(.htEnumMoviePlayCl(var_movie_id: var_animeInfo!.var_id, var_movie_name: var_animeInfo!.var_title ?? "", var_kid: "8", var_source: var_source, var_eps_id: var_selEp.var_id ?? "", var_eps_name: var_selEp.var_title ?? "", var_movielist_id: "", var_secname: "", var_subtitle: "1", var_m_type: "2", var_ori_type: "1"))
          })
          Button {
            var_showPushToFeedback = true
            HTNetworkManager().ht_requestForBuried(.htEnumMoviePlayCl(var_movie_id: var_animeInfo!.var_id, var_movie_name: var_animeInfo!.var_title ?? "", var_kid: "60", var_source: var_source, var_eps_id: var_selEp.var_id ?? "", var_eps_name: var_selEp.var_title ?? "", var_movielist_id: "", var_secname: "", var_subtitle: "1", var_m_type: "2", var_ori_type: "1"))
          } label: {
            Image("feedback")
              .resizable()
              .frame(width: 22, height: 22)
              .padding(.leading, 27)
          }
        }
        .padding(.top, 29)
      }
      .padding(.horizontal, 15)
    }
  }
}
